package com.minivision.sdktestdemo.ui;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.minivision.sdktestdemo.R;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by devtang on 2016/5/24.
 */
public class WrapAdapter extends RecyclerView.Adapter {
    private RecyclerView.Adapter mWrapAdapter;

    public WrapAdapter(RecyclerView.Adapter adapter) {
        mWrapAdapter = adapter;
    }

    public class ContentView {
        public static final int HEAD_TYPE = 1 << 11;
        public static final int FOOT_TYPE = 1 << 12;
        public int mViewType;
        public int mViewId;
        public View mView;
    }

    private ContentView mHeaderView;
    private OnHeadClickListener mHeaderClickListener;
    private ContentView mFooterView;


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (isHead(viewType)) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_item_head, parent, false);
            mHeaderView.mView = view;
            return new HeadViewHolder(view);
        } else if (isFooter(viewType)) {
            //View view = mFooterViews.get(viewType - ContentView.FOOT_TYPE).mView;
            throw new IllegalStateException("暂时没有尾部");
//            return new HeadViewHolder(view);
        } else {
            return mWrapAdapter.onCreateViewHolder(parent, viewType);
        }
    }

    public boolean isHead(int viewType) {
        return viewType >= ContentView.HEAD_TYPE && viewType < ContentView.HEAD_TYPE + (mHeaderView == null ? 0 : 1);
    }

    public boolean isFooter(int viewType) {
        return viewType >= ContentView.FOOT_TYPE && viewType < ContentView.FOOT_TYPE + (mFooterView == null ? 0 : 1);
    }

    public boolean isHeadItem(int position) {
        return position < (mHeaderView == null ? 0 : 1);
    }

    public boolean isFootItem(int position) {
        return position >= (mHeaderView == null ? 0 : 1) + mWrapAdapter.getItemCount();
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (isHeadItem(position)) {
            HeadViewHolder headViewHolder = (HeadViewHolder) holder;
            headViewHolder.mTvSongs.setText("(共" + mWrapAdapter.getItemCount() + "首)");
            if (mHeaderClickListener != null) {
                headViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mHeaderClickListener.onClickHead();
                    }
                });
                headViewHolder.mListIcon.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mHeaderClickListener.onClickHeadMore();
                    }
                });
            }
        } else if (isFootItem(position)) {

        } else {
            int pos = position - getHeadSize();
            mWrapAdapter.onBindViewHolder(holder, pos);
        }
    }

    public int getHeadSize() {
        return mHeaderView == null ? 0 : 1;
    }

    public int getFootSize() {
        return mFooterView == null ? 0 : 1;
    }

    @Override
    public int getItemCount() {
        int count = getFootSize() + getHeadSize() + mWrapAdapter.getItemCount();
        return count;
    }

    public boolean isEmpty() {
        return getItemCount() == getFootSize() + getHeadSize();
    }

    public void setHeadViewId(int id) {
        ContentView contentView = new ContentView();
        contentView.mViewId = id;
        contentView.mViewType = ContentView.HEAD_TYPE;
        mHeaderView = contentView;
    }

    public void setFootViewId(int id) {
        ContentView contentView = new ContentView();
        contentView.mViewId = id;
        contentView.mViewType = ContentView.FOOT_TYPE;
        mFooterView = contentView;
    }

    public interface OnHeadClickListener {
        void onClickHead();
        void onClickHeadMore();
    }

    public void setHeadVisiable(boolean flag) {
        if (mHeaderView != null) {
            mHeaderView.mView.setVisibility(flag ? View.VISIBLE : View.GONE);
        }
    }

    public void setHeadListener(OnHeadClickListener listener) {
        mHeaderClickListener = listener;
    }

    public class HeadViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.tv_songs)
        TextView mTvSongs;

        @Bind(R.id.icon_list)
        SimpleDraweeView mListIcon;

        @Bind(R.id.icon_play)
        SimpleDraweeView mPlayIcon;

        public HeadViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (isHeadItem(position)) {
            return ContentView.HEAD_TYPE;
        } else if (isFootItem(position)) {
            return ContentView.FOOT_TYPE;
        } else {
            return mWrapAdapter.getItemViewType(position - getHeadSize());
        }
    }
}
